﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.BoM;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.Process;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.BoM
{
    /// <summary>
    /// 根据物料ID获取物料清单和工艺路线信息Handler
    /// </summary>
    public class SearchBOMInforIDHandler : IRequestHandler<SearchBOMInforIDCommand, Result<List<MaterialIdDto>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 物料管理表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialManagement> materialManagementrepository;
        /// <summary>
        /// 物料清单表仓储
        /// </summary>
        public readonly IBaseRepository<BoMInform> boMInformrepository;
        /// <summary>
        /// 工艺路线表仓储
        /// </summary>
        public readonly IBaseRepository<ProcessRoutes> processRoutesrepository;
        /// <summary>
        /// 物料单位表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialUnit> materialUnitrepository;
        /// <summary>
        /// 规格单位表仓储
        /// </summary>
        public readonly IBaseRepository<SpecificationUnit> specificationUnitrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchBOMInforIDHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="mapper"></param>
        /// <param name="materialManagementrepository"></param>
        /// <param name="boMInformrepository"></param>
        /// <param name="processRoutesrepository"></param>
        /// <param name="logger"></param>
        /// <param name="materialUnitrepository"></param>
        /// <param name="specificationUnitrepository"></param>
        public SearchBOMInforIDHandler(ISqlSugarClient db, IMapper mapper, IBaseRepository<MaterialManagement> materialManagementrepository, IBaseRepository<BoMInform> boMInformrepository, IBaseRepository<ProcessRoutes> processRoutesrepository, ILogger<SearchBOMInforIDHandler> logger, IBaseRepository<MaterialUnit> materialUnitrepository, IBaseRepository<SpecificationUnit> specificationUnitrepository)
        {
            _db = db;
            this.mapper = mapper;
            this.materialManagementrepository = materialManagementrepository;
            this.boMInformrepository = boMInformrepository;
            this.processRoutesrepository = processRoutesrepository;
            this.logger = logger;
            this.materialUnitrepository = materialUnitrepository;
            this.specificationUnitrepository = specificationUnitrepository;
        }
        /// <summary>
        /// 根据物料ID获取物料清单和工艺路线信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<List<MaterialIdDto>>> Handle(SearchBOMInforIDCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<MaterialIdDto>>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<MaterialManagement, BoMInform, ProcessRoutes, MaterialUnit, SpecificationUnit>((a, b, c, d, e) => new JoinQueryInfos(
                     JoinType.Inner, a.MaterialId == b.MaterialId,
                     JoinType.Inner, a.MaterialId == c.MaterialId,
                     JoinType.Inner, a.MaterialUnitId == d.MaterialUnitId,
                     JoinType.Inner, a.SpecificationUnitId == e.SpecificationUnitId
                     ))
                     .Select((a, b, c, d, e) => new MaterialIdDto()
                     {
                         MaterialId = a.MaterialId,
                         MaterialName = a.MaterialName,
                         MaterialCode = a.MaterialCode,
                         BoMInformId = b.BoMInformId,
                         BoMInformCode = b.BoMInformCode,
                         BoMInformVersion = b.BoMInformVersion,
                         SingleMaker = b.SingleMaker,
                         ProcessRouteId = c.ProcessRouteId,
                         ProcessRouteName = c.ProcessRouteName,
                         ProcessVersion = c.ProcessVersion,
                         Specifications = a.Specifications,
                         MaterialUnitId = d.MaterialUnitId,
                         MaterialUnitName = d.MaterialUnitName,
                         SpecificationUnitId = e.SpecificationUnitId,
                         SpecificationUnitName = e.SpecificationUnitName,
                         PackagingSpecifications = a.PackagingSpecifications,
                         IsStatus = a.IsStatus,
                         IsDel = a.IsDel,
                         AuditStatus = a.AuditStatus,
                         CreatedOwner = a.CreatedOwner,
                         CreatedTime = a.CreatedTime
                     }).Where(a => a.IsDel == 1).ToListAsync();
                if (request.MaterialId != 0)
                {
                    query = query.Where(a => a.MaterialId == request.MaterialId).ToList();
                }
                result.Data = query;
                result.Msg = "根根据物料ID获取物料清单和工艺路线信息查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据物料ID获取物料清单和工艺路线信息查询失败" + ex.Message;
                logger.LogError("根据物料ID获取物料清单和工艺路线信息查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
